package top.gangster.lunan.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import top.gangster.lunan.userservice.entity.Department;
import top.gangster.lunan.userservice.entity.Role;
import top.gangster.lunan.userservice.entity.User;
import top.gangster.lunan.userservice.entity.UserAdd;
import top.gangster.lunan.userservice.entity.UserSave;
import top.gangster.lunan.userservice.entity.vo.UserVo;
import top.gangster.lunan.userservice.mapper.DepartmentMapper;
import top.gangster.lunan.userservice.mapper.RoleMapper;
import top.gangster.lunan.userservice.mapper.UserMapper;
import top.gangster.lunan.userservice.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 使用这个系统的用户表，包含用户的登录与基础信息 服务实现类
 * </p>
 *
 * @author vic
 * @since 2021-11-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Autowired
  UserService userService;
  @Resource
  UserMapper userMapper;
  @Resource
  DepartmentMapper departmentMapper;
  @Resource
  RoleMapper roleMapper;

  /**
   * 根据用户id获取用户
   * @param userId 用户id
   * @return User
   */
  @Override
  public User getUser(String userId){
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    User res = userService.getOne(queryWrapper.eq(User::getUserId,userId));
    if (res == null){
      return null;
    }
    return res;
  }

  /**
   * 添加用户
   * @param users 添加用户参数
   * @return boolean
   */
  @Override
  public boolean addUser(UserAdd users){
    User res = new User();
    String deptNameTemp = users.getDeptName();
    String roleNameTemp = users.getRoleName();
    LambdaQueryWrapper<Department> queryWrapperDept = new LambdaQueryWrapper<>();
    Department deptTemp = departmentMapper.selectOne(queryWrapperDept.eq(Department::getDeptName,deptNameTemp));
    if (deptTemp == null)
    {
      return false;
    }
    String deptIdTemp = deptTemp.getDepptId();
    LambdaQueryWrapper<Role> queryWrapperRole = new LambdaQueryWrapper<>();
    Role roleTemp = roleMapper.selectOne(queryWrapperRole.eq(Role::getRoleName,roleNameTemp));
    if (roleTemp == null)
    {
      return false;
    }
    String roleIdTemp = roleTemp.getRoleId();
    res.setAvatar(users.getAvatar());
    res.setDeptId(deptIdTemp);
    res.setPassword(users.getPassword());
    res.setUsername(users.getUsername());
    res.setIsEnable(users.getIsEnable());
    res.setRoleId(roleIdTemp);
    res.setPhoneNumber(users.getPhoneNumber());
//    LambdaQueryWrapper<Department> queryWrapperDept = new LambdaQueryWrapper<>();
//    Department deptTemp = departmentMapper.selectOne(queryWrapperDept.eq(Department::getDeptName,deptNameTemp));
    boolean b = userService.save(res);
    return b;
  }

  /**
   * 修改用户
   * @param users 修改用户参数
   * @return boolean
   */
  @Override
  public boolean modifyUser(UserAdd users){
    User res = new User();
    String deptNameTemp = users.getDeptName();
    String roleNameTemp = users.getRoleName();
    LambdaQueryWrapper<Department> queryWrapperDept = new LambdaQueryWrapper<>();
    Department deptTemp = departmentMapper.selectOne(queryWrapperDept.eq(Department::getDeptName,deptNameTemp));
    if (deptTemp == null)
    {
      return false;
    }
    String deptIdTemp = deptTemp.getDepptId();
    LambdaQueryWrapper<Role> queryWrapperRole = new LambdaQueryWrapper<>();
    Role roleTemp = roleMapper.selectOne(queryWrapperRole.eq(Role::getRoleName,roleNameTemp));
    if (roleTemp == null)
    {
      return false;
    }
    String roleIdTemp = roleTemp.getRoleId();
    res.setUserId(users.getUserId());
    res.setAvatar(users.getAvatar());
    res.setDeptId(deptIdTemp);
    res.setPassword(users.getPassword());
    res.setUsername(users.getUsername());
    res.setIsEnable(users.getIsEnable());
    res.setRoleId(roleIdTemp);
    res.setPhoneNumber(users.getPhoneNumber());
    boolean b = userService.updateById(res);
    return b;
  }

  /**
   * 删除用户
   * @param users 删除用户参数
   * @return boolean
   */
  @Override
  public boolean deleteUser(UserAdd users){
    String deleteId = users.getUserId();
    boolean b = userService.removeById(deleteId);
    return b;
  }

  /**
   * 冻结用户
   * @param users 冻结用户参数
   * @return boolean
   */
  @Override
  public boolean freezeUser(User users){
    boolean b = userService.updateById(users);
    return b;
  }

  /**
   * 验证用户
   * @param uid 用户工号
   * @param password 密码
   * @return User
   */
  @Override
  public User verifyUser(String uid, String password) {
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    System.out.println(uid+"service层");
    System.out.println(password+"service层");
    User res = userService.getOne(queryWrapper.
        eq(User::getUid,uid).
        eq(User::getPassword,password));
    if (res == null){
      System.out.println("没有res");
      return null;
    }

    return res;
  }

  /**
   * 拿到用户列表
   * @param current 当前页
   * @param size 当前页大小
   * @return UserVo
   */
  @Override
  public UserVo getUserList(Integer current,Integer size){
    UserVo userVo = new UserVo();
    Page<UserSave> page = new Page<>(current, size);
    IPage<UserSave> res = userMapper.selectPageUserList(page);
    userVo.setUserList(page.getRecords());
    userVo.setCurrent(current);
    userVo.setSize(size);
    userVo.setTotal(page.getTotal());
    return userVo;
  }

  /**
   * 通过用户名查找用户
   * @param current 当前页
   * @param size 当前页大小
   * @param username 用户
   * @return UserVo
   */
  @Override
  public UserVo getUserByName(Integer current,Integer size,String username){
    UserVo userVo = new UserVo();
    Page<UserSave> page = new Page<>(current,size);
    IPage<UserSave> res = userMapper.selectPageUserByName(page,username);
    userVo.setSize(size);
    userVo.setUserList(page.getRecords());
    userVo.setCurrent(current);
    userVo.setTotal(page.getTotal());
    return userVo;
  }

  /**
   * 通过电话号码查找用户
   * @param current 当前页
   * @param size 当前页大小
   * @param phonenumber 电话
   * @return UserVo
   */
  @Override
  public UserVo getUserByPhone(Integer current,Integer size,String phonenumber){
    UserVo userVo = new UserVo();
    Page<UserSave> page = new Page<>(current,size);
    IPage<UserSave> res = userMapper.selectPageUserByPhone(page,phonenumber);
    userVo.setUserList(page.getRecords());
    userVo.setCurrent(current);
    userVo.setTotal(page.getTotal());
    userVo.setSize(size);
    return userVo;
  }

  /**
   * 通过部门名字查找用户
   * @param current 当前页
   * @param size 当前页
   * @param deptname 部门
   * @return UserVo
   */
  @Override
  public UserVo getUserByDept(Integer current,Integer size,String deptname){
    UserVo userVo = new UserVo();
    Page<UserSave> page = new Page<>(current,size);
    IPage<UserSave> res = userMapper.selectPageUserByDept(page,deptname);
    userVo.setSize(size);
    userVo.setUserList(page.getRecords());
    userVo.setTotal(page.getTotal());
    userVo.setCurrent(current);
    return userVo;
  }

  /**
   * 通过角色查找用户分页版本
   * @param current 当前页
   * @param size 当前页大小
   * @param role 角色
   * @return UserVo
   */
  @Override
  public UserVo getUserByRole(Integer current,Integer size,String role){
    UserVo userVo = new UserVo();
    Page<UserSave> page = new Page<>(current,size);
    IPage<UserSave> res = userMapper.selectPageUserByRole(page,role);
    userVo.setCurrent(current);
    userVo.setUserList(page.getRecords());
    userVo.setTotal(page.getTotal());
    userVo.setSize(size);
    return userVo;
  }

  /**
   * 通过角色查找用户不分页版本
   * @param roleId 角色id
   * @return List<UserSave>
   */
  @Override
  public List<UserSave> getUserByRole(String roleId){
    return userMapper.selectUserByRole(roleId);
  }

  /**
   * 通过部门查找用户不分页版本
   * @param deptId 部门id
   * @return List<UserSave>
   */
  @Override
  public List<UserSave> getUserByDept(String deptId){
    return userMapper.selectUserByDept(deptId);
  }
}
